﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ThiTracNghiem
{
    public partial class FormMain : Form
    {
        FormLogin frmLogin;
        public FormMain()
        {
            InitializeComponent();
        }

        private void đăngNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin = new FormLogin();
            if (frmLogin.ShowDialog() == DialogResult.OK)
            {
                doAfterLogin();
            }
            else
            {
                menuLogin.Enabled = true;
                menuThoat.Enabled = false;
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (frmLogin == null || frmLogin.IsDisposed)
            {
                frmLogin = new FormLogin();
                if (frmLogin.ShowDialog() == DialogResult.OK)
                {
                    doAfterLogin();
                }
                else
                {
                    menuLogin.Enabled = true;
                    menuThoat.Enabled = false;
                }
            }            
        }

        public void doAfterLogin()
        {
            menuLogin.Enabled = false;
            menuThoat.Enabled = true;
            String roleName = "";

            using (SqlConnection con = new SqlConnection(Program.ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SP_GET_CURRENT_ROLE", con))
                {
                    try
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        con.Open();
                        SqlDataReader reader = cmd.ExecuteReader();
                        if (reader.Read())
                        {
                            roleName = reader[1].ToString().Trim();
                        }
                        reader.Close();
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("Lỗi không xác định.\nChi tiết: " + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    finally
                    {
                        con.Close();
                    }
                }
            }

            if (roleName.ToUpper().Equals("ADMIN"))
            {
                menuAdmin.Enabled = true;
                menuGV.Enabled = true;
                menuSV.Enabled = true;
                status.Text = "Xin chào, Admin: " + Program.StatusInfo;
            }
            else if (roleName.ToUpper().Equals("GIAOVIEN"))
            {
                menuAdmin.Enabled = false;
                menuGV.Enabled = true;
                status.Text = "Xin chào, giảng viên " + Program.StatusInfo;
                menuSV.Enabled = true;
            }
            else if (roleName.ToUpper().Equals("SINHVIEN"))
            {
                menuAdmin.Enabled = false;
                menuGV.Enabled = false;
                menuSV.Enabled = true;
                status.Text = "Sinh viên: " + Program.StatusInfo;
            }
            else
            {
                menuAdmin.Enabled = false;
                menuGV.Enabled = false;
                menuSV.Enabled = false;
            }
        }

        private void quảnLýGiảngViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            FormQLDSGiaoVien frmGiangVien = new FormQLDSGiaoVien();
            
            frmGiangVien.Show();
        }

        private void quảnLýSinhViênToolStripMenuItem1_Click(object sender, EventArgs e)
        {
 
            FormQLDSSinhVien frmSinhVien = new FormQLDSSinhVien();
            frmSinhVien.Show();
        }

        private void quảnLýLớpToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FormQLDSLop frmLop = new FormQLDSLop();
            frmLop.Show();
        }

        private void quảnLýMônHọcToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            FormQLMonHoc frmMonHoc = new FormQLMonHoc();
            frmMonHoc.Show();
        }


        private void đổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            FormDoiPassword frmPassword = new FormDoiPassword();
            frmPassword.Show();
        }

        private void menuThoat_Click(object sender, EventArgs e)
        {
            Program.ConnectionString = Program.OriginalConString;
            menuAdmin.Enabled = false;
            menuGV.Enabled = false;
            menuSV.Enabled = false;
            menuThoat.Enabled = false;
            status.Text = "";

            FormMain_Load(null, null);
        }

        private void saoLưuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormBackup frmBackup = new FormBackup();
            frmBackup.Show();
        }

        private void quảnLíThiếtBịSaoLưuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormDeviceMgr frmDevMgr = new FormDeviceMgr();
            frmDevMgr.Show();
        }

        private void phụcHồiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormRestore frmRestore = new FormRestore();
            frmRestore.Show();
        }
    }
}
